//
//  AppDelegate.m
//  CoreData-Demo
//
//  Created by 赵国腾 on 2020/4/20.
//  Copyright © 2020 gaven. All rights reserved.
//

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    
    [self addTestData];
    
    return YES;
}

#pragma mark - Core Data stack

- (Item *)rootItem {

    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Item"];
    request.predicate = [NSPredicate predicateWithFormat:@"parent = %@", nil];
    
    NSError *error = nil;
    NSArray *objcs = [self.managedObjectContext executeFetchRequest:request error: &error];
    
    if (error) {
        NSLog(@"##### error = %@", error);
    }
    
    return nil;
}

- (void)setupManagedObjectContext {
    
    // 用于处理和跟踪对托管对象的更改的对象空间。
    self.managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
    
    NSManagedObjectModel *managedObjectModel = [[NSManagedObjectModel alloc] init];
    
    // 存储协调
    self.managedObjectContext.persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: managedObjectModel];
    
    NSError *error = nil;
    [self.managedObjectContext.persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                                                  configuration:nil
                                                                            URL:nil
                                                                        options:nil
                                                                          error:&error];
    
    if (error) {
        NSLog(@"error: %@", error);
    }
    
    self.managedObjectContext.undoManager = [[NSUndoManager alloc] init];
}

- (void)addTestData {
    
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Device" inManagedObjectContext:self.persistentContainer.viewContext];
    
    if (entity) {
        NSManagedObject *managedObject = [[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:self.persistentContainer.viewContext];
        [managedObject setValue:@"gaven" forKey:@"name"];
    }
    
    
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Device"];
    NSArray *res = [self.persistentContainer.viewContext executeFetchRequest:fetchRequest error:nil];
    NSLog(@"###### %@", res);
}


@synthesize persistentContainer = _persistentContainer;

- (NSPersistentContainer *)persistentContainer API_AVAILABLE(ios(10.0)) {
    @synchronized (self) {
        if (_persistentContainer == nil) {
            _persistentContainer = [[NSPersistentContainer alloc] initWithName:@"CoreData_Demo"];
            [_persistentContainer loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *storeDescription, NSError *error) {
                if (error != nil) {
                    NSLog(@"Unresolved error %@, %@", error, error.userInfo);
//                    abort();
                }
            }];
        }
    }

    return _persistentContainer;
}

#pragma mark - Core Data Saving support

- (void)saveContext {
    NSManagedObjectContext *context = self.persistentContainer.viewContext;
    NSError *error = nil;
    if ([context hasChanges] && ![context save:&error]) {
        NSLog(@"Unresolved error %@, %@", error, error.userInfo);
//        abort();
    }
}

@end
